------------------基础配置表-------------
local config = {}
--------配置GameAnalytics路径
config.Ga_Path = nil

--------配置Icon锚点 Vector2.new(0,0)
config.Icon_AnchorPoint = Vector2.new(1,0.5)
--------配置Icon大小 UDim2.fromScale
config.Icon_Size = UDim2.fromScale(0.08,0.08)
--------配置Icon位置 UDim2.new(0,0,0,0)
config.Icon_Position = UDim2.new(0.996,0,0.1,0)

-----------------加载服务--------------- 
local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--------------------加载事件-----------------
local Event = Instance.new("RemoteEvent", ReplicatedStorage)
Event.Name = "GameAnalyticsEvent"
local FuncEvent = Instance.new("RemoteFunction", ReplicatedStorage)
FuncEvent.Name = "GameAnalyticsFuncEvent"
--------------------从网站拉取游戏中心界面配置表-------------

local response,game_config
local r = pcall(function()
	response = HttpService:GetAsync("http://www.poorandstrange.com/OHOHConfig.txt", true)	
end)

if r then

	pcall(function()
		game_config = HttpService:JSONDecode(response)
	end)
end


---@type fun()691200
local  HttpGetConfig = coroutine.wrap(function()
	local r,e,old_timer = true,691200,os.time()
	while r do

		wait()
		if os.time() - old_timer >= e then	
			xpcall(function()
				response = HttpService:GetAsync("http://www.poorandstrange.com/OHOHConfig.txt", true)
				game_config = HttpService:JSONDecode(response)
			end,debug.traceback)
			
			old_timer = os.time()
		end
	end
end)
---------------------------加载界面----------------------
---@type fun(inst_path:string,num:number):LuaSourceContainer
---@param inst_path string
---@param num number
---@return LuaSourceContainer
local function WaitChildProcess(inst_path,num)
	local r,e = true,false
	local i = num or 300
	while r do
		r,e = xpcall(function()
			return inst_path
		end,debug.traceback)
		wait()
		i = i - 1
		if i <= 0 then
			return false
		end
	end
end
---@type fun(inst_path:string):LuaSourceContainer
---@param inst_path string
---@return LuaSourceContainer
local function RequireProcess(inst_path)
	local r,e = true,false
	while r do
		r,e = xpcall(function()
			return require(inst_path)
		end,debug.traceback)
		wait()
	end
end
local Config_Module

local function LoadGUI()
	local r,e = xpcall(function()
		local f = coroutine.wrap(function()
			
			while not game_config  do
				wait(8)
				local s= xpcall(function()
					response = HttpService:GetAsync("http://www.poorandstrange.com/OHOHConfig.txt", true)
					game_config = HttpService:JSONDecode(response)
				end,debug.traceback)
			end
			
			--warn("moveGame-->: 存在game_config")

			---加载UI
			local game_gui = script:WaitForChild("GameAnalyticsGui")
			if not game_gui then
				game_gui = WaitChildProcess(script:WaitForChild("GameAnalyticsGui"))
			end
			---加载配置表
			local Config_Inst = game_gui:WaitForChild("Config")
			
			if Config_Inst then
				Config_Module = require(Config_Inst)
			else
				Config_Module = RequireProcess(game_gui:WaitForChild("Config",10))    
			end
			Config_Module.games = game_config
			game_gui:SetAttribute("HudPos",(config.Icon_Position or UDim2.new(0.015,0,0.454,0) ))
			local hud_icon = game_gui:WaitForChild("HUDIcon")
			hud_icon.AnchorPoint =config.Icon_AnchorPoint or Vector2.new(0.5,0.5)
			hud_icon.Size = config.Icon_Size or UDim2.fromScale(0.08,0.08)
			hud_icon.Position = config.Icon_Position or UDim2.new(0.015,0,0.454,0)
			
			local player_list = game:GetService("Players"):GetPlayers()
			-----服务器已存在的玩家
			local clone_inst,player_gui,new_game_gui
			for _,player in pairs(player_list) do
				if not player then
					continue
				end
				player_gui = player:WaitForChild("PlayerGui")
				if not player_gui then
					player_gui = WaitChildProcess(player:WaitForChild("PlayerGui",5),10)
					if not player_gui then
						continue 
					end
				end
				clone_inst = game_gui:Clone()
				clone_inst.Parent = player.PlayerGui
			end
			game:GetService("Players").PlayerAdded:Connect(function(player)
				pcall(function()
					if not player then
						return 
					end
					player_gui = player:WaitForChild("PlayerGui")
					if not player_gui  then
						player_gui = WaitChildProcess(player:WaitForChild("PlayerGui",5),10)
						if not player_gui then
							return 
						end
					end
					clone_inst = game_gui:Clone()
					clone_inst.Parent = player.PlayerGui
				end)
			end)
		end)
		f()
	end,debug.traceback)
	if not r then
		warn(e)
	end
end
---调用方法
LoadGUI()
HttpGetConfig()

-------------------------配置GameAnalytics-------------------
local GameAnalytics
local c = {}
---@type fun(class_inst:LuaSourceContainer)
---@param class_inst LuaSourceContainer
function c:LoadGameAnalytics(class_inst)
	GameAnalytics = class_inst
	if GameAnalytics then
		return
	end
	if not config.Ga_Path then
		warn("未填写GA路径")
	end
	local r,e = xpcall(function()
		local f = coroutine.wrap(function()
			GameAnalytics = require(config.Ga_Path)
		end)
		f()
	end,debug.traceback)
	if not r then
		warn(e)
	end
end

------------------------事件------------------
local function GameAnalyticsEvent(player, str)
	if not player then
		warn("传入的player为空！")
	end
	-- GameAnalytics:addDesignEvent(player.UserId,{
	-- 	eventId = "Game:"..str,
	-- 	value = 34.5
	-- })
end
Event.OnServerEvent:Connect(GameAnalyticsEvent)


local function onCreatePartRequested(player)
	--warn("moveGame onCreatePartRequested--->收到")

	if not player then
		-- warn("传入的player为空！")
	end
	return Config_Module
end

FuncEvent.OnServerInvoke = onCreatePartRequested
--warn("moveGame onCreatePartRequested--->注册")

return c


